#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include "fileoperation.h"
#include <QFileInfo>
#include <QFileInfoList>
#include <QRegExp>
#include <QByteArray>
#include <QFile>
#include <QDir>
#include <QIODevice>
#include <QDataStream>
#include <QMap>
#include <QFileInfo>
#include <QFileInfoList>
#include <QRegExp>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->lineEdit_password->setEchoMode(QLineEdit::Password);
    QStringList arg = QApplication::arguments();
    if (arg.size() > 1)
        ui->lineEdit_file->setText(arg.at(1));
    this->progressBar = new QProgressBar();
    progressBar->setRange(0, 100);
    progressBar->hide();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::startProgress(double progress)
{
        progressBar->setValue(progress * 100);
        progressBar->update();
}

void MainWindow::on_pushButton_cancel_clicked()
{
    //passwordError = new Error(this);
    //passwordError->show();
    close();
}

void MainWindow::on_pushButton_decode_clicked()
{
    QString pwd = ui->lineEdit_password->text();
    QString filePath = ui->lineEdit_file->text();
    QString decodePath = ui->lineEdit_location->text();
    if (pwd == "" || filePath == "" || decodePath == "") {
        QMessageBox::information(NULL, "Decode", "信息输入不全",QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        return;
    }
    FileOperation fo(filePath, decodePath);
    progressBar->show();
    connect(&fo, SIGNAL(sendPro(double)), this, SLOT(startProgress(double)));
    switch(fo.handleFileDecryption(pwd)){
    case 0:
        close();
        QMessageBox::information(NULL, "Decode", "解密成功！",QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        break;
    case 1:
        QMessageBox::information(NULL, "Decode", "文件类型错误！",QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        break;
    case 2:
        QMessageBox::information(NULL, "Decode", "密码错误！",QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        break;
    case 3:
        QMessageBox::information(NULL, "Decode", "文件结构已损坏！",QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        break;
    case 4:
        QMessageBox::information(NULL, "Decode", "解密失败！",QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
    }
}

void MainWindow::on_pushButton_choose_clicked()
{
    QString srcPath = QFileDialog::getExistingDirectory(this, "choose src Directory","/");
    if(srcPath.isEmpty())
    {
        return;
    }else {
        ui->lineEdit_location->setText(srcPath);}
}

void MainWindow::on_pushButton_chooseFile_clicked()
{
    QString filePath = QFileDialog::getOpenFileName(this, tr("Open File"),"/home");
    if(filePath.isEmpty())
    {
        return;
    }else {
        ui->lineEdit_file->setText(filePath);
}
}
